
#include "stemmer.h"

Stemmer::Stemmer(const char * dictPath)
{
	cout << "Init stemmer dictionary...";
	string s;
	myutil::readFile(dictPath, s);
	boost::algorithm::trim(s);

	vector<string> res;
	myutil::splitString(s.c_str(), res, '\n');

	for(auto p = res.begin(); p != res.end(); p++)
	{
		boost::algorithm::trim(*p);
		if ((*p).size() == 0)
		{
			continue;
		}

		vector<string> tmp;
		myutil::splitString((*p).c_str(), tmp, '\t');
		if (tmp.size() != 2)
		{
			cout << "dict file error" << endl;
			continue;
		}

		boost::algorithm::trim(tmp[0]);
		boost::algorithm::trim(tmp[1]);
		tmp[0].shrink_to_fit();
		tmp[1].shrink_to_fit();
		m_toStem.insert(make_pair(tmp[0], tmp[1]));
	}
	cout << m_toStem.size() << "pair got." << endl;
}

Stemmer::~Stemmer()
{}

string Stemmer::getStem(const string & word)const
{
	auto p = m_toStem.find(word);
	if (p == m_toStem.end())
	{
		return word;
	}
	return p->second;
}
